package com.wifi.a007.wifiFragment;

import android.content.SharedPreferences;
import android.preference.Preference;
import android.preference.PreferenceManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.view.menu.ExpandedMenuView;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.wifi.a007.R;
import com.wifi.a007.ViewName;

import java.util.ArrayList;
import java.util.List;

import javax.security.auth.Destroyable;

public class HtmlMenu extends AppCompatActivity implements  View.OnClickListener{

    private ListView listMenu;
    private ArrayAdapter<String> adapter;
    private List<String> listData=new ArrayList<>();
    private Button    addButton;
    private CheckBox   checkBox1;
    private EditText   urlName;
    private LinearLayout  urlLinear;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_html_menu);


        checkBox1 = (CheckBox)findViewById(R.id.checkBox1);
        addButton =(Button)findViewById(R.id.addUrl);
        urlName   =(EditText)findViewById(R.id.urlName);
        urlLinear =(LinearLayout)findViewById(R.id.addLinear);
        checkBox1.setOnClickListener(this);
        addButton.setOnClickListener(this);



        listMenu =(ListView)findViewById(R.id.list_html_menu);

        initListData();
        adapter = new ArrayAdapter<>(HtmlMenu.this,android.R.layout.simple_list_item_1,listData);
        listMenu.setAdapter(adapter);

        listMenu.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                String url=listData.get(position);
                ViewName.webView.loadUrl(url);
                finish();
            }
        });

    }

    private void initListData(){


      SharedPreferences pf= PreferenceManager.getDefaultSharedPreferences(HtmlMenu.this);
        listData.add("http://www.baidu.com");
        listData.add("http://192.168.201.4/androidsource/docs/");
        listData.add("http://www.52pojie.cn");
        listData.add("http://www.bbc.com/news");
        listData.add("https://www.opengps.cn/Data/IP/LocHighAcc.aspx");
        listData.add("http://192.168.201.4/arm.php");
        int count = pf.getInt("count",0);

        for(int i=1; i<= count ;i++){

        String menu= pf.getString(""+i,"nothing");

        listData.add(menu);

        }




    }


    @Override
    public void onClick(View v) {

        switch (v.getId()){

            case R.id.checkBox1:
                if( urlLinear.getVisibility() == View.GONE){


                    urlLinear.setVisibility(View.VISIBLE);
                    urlName.setText("http://");

                }else{

                  urlLinear.setVisibility(View.GONE);
                }

                break;

            case R.id.addUrl:


                String url=urlName.getText().toString();
                if(addData(url)) {
                    checkBox1.setChecked(false);
                    urlLinear.setVisibility(View.GONE);
                    Toast.makeText(HtmlMenu.this,"添加成功",Toast.LENGTH_SHORT).show();

                }else{
                    Toast.makeText(HtmlMenu.this,"添加失败",Toast.LENGTH_SHORT).show();

                }


                break;

            case  R.id.urlName:



                break;

            default:
                break;






        }


    }

    private  boolean addData(String urlName){
        try {
            SharedPreferences.Editor editor = PreferenceManager.getDefaultSharedPreferences(HtmlMenu.this).edit();
            SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(HtmlMenu.this);

            int currentCount = preferences.getInt("count", 0);

            editor.putInt("count",currentCount+1);
            editor.putString(""+(currentCount + 1), urlName);
            editor.apply();
            listData.add(urlName);
            adapter.notifyDataSetChanged();

        }catch (Exception e){

            e.printStackTrace();
            return false;
        }




        return true;

    }


    @Override
    protected void onDestroy() {

        Log.i("alert", "destory");
        super.onDestroy();
    }
}
